package com.maya.model;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Reference;


@Entity(value = "users")
public class User implements Idable {
    @Id
    private ObjectId id;
    private String email;
    private String name;
    private String tagline;
    private String bio;
    private String website;
    private boolean girl;
    private String password;
    private int askCount;
    private int answerCount;
    @Reference
    private TimeLine timeline;
    // 提示：
    // 被关注的问题 话题
    private List<ObjectId> followedQuestionIds = new ArrayList<ObjectId>(); // 两边都存储
    private List<ObjectId> followedTopicIds = new ArrayList<ObjectId>();// 两边都存储

    // SNS的follow关系双向都存储，利于查询，保存时同时更新两边
    // 1。不采取@Reference是因为Morphia自动查询出引用的list,这样就会
    // 从数据库中抽取出巨大的冗余数据
    // 2。也可以用Reference lazy=true来解决上面的问题，
    // 衡量下id和DBRef，我觉得还是存储id比较经济
    private List<ObjectId> followingIds = new ArrayList<ObjectId>();
    private List<ObjectId> followerIds = new ArrayList<ObjectId>();

    
    
    
    public TimeLine getTimeline() {
        return timeline;
    }

    public void setTimeline(TimeLine timeline) {
        this.timeline = timeline;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTagline() {
        return tagline;
    }

    public void setTagline(String tagline) {
        this.tagline = tagline;
    }

    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public boolean isGirl() {
        return girl;
    }

    public void setGirl(boolean girl) {
        this.girl = girl;
    }

    public int getAskCount() {
        return askCount;
    }

    public void setAskCount(int askCount) {
        this.askCount = askCount;
    }

    public int getAnswerCount() {
        return answerCount;
    }

    public void setAnswerCount(int answerCount) {
        this.answerCount = answerCount;
    }

    public List<ObjectId> getFollowedQuestionIds() {
        return followedQuestionIds;
    }

    public void setFollowedQuestionIds(List<ObjectId> followedQuestionIds) {
        this.followedQuestionIds = followedQuestionIds;
    }

    public List<ObjectId> getFollowedTopicIds() {
        return followedTopicIds;
    }

    public void setFollowedTopicIds(List<ObjectId> followedTopicIds) {
        this.followedTopicIds = followedTopicIds;
    }

    public List<ObjectId> getFollowingIds() {
        return followingIds;
    }

    public void setFollowingIds(List<ObjectId> followingIds) {
        this.followingIds = followingIds;
    }

    public List<ObjectId> getFollowerIds() {
        return followerIds;
    }

    public void setFollowerIds(List<ObjectId> followerIds) {
        this.followerIds = followerIds;
    }
    
 // 只比较两个user是不是有相同的ObjectId
    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || !(obj instanceof User)) return false;
        User otherUser = (User) obj;
        return this.id.equals(otherUser.id);
    }

    // 对objectId进行hash, 当然这对存在于数据库中并且已经分配好id的user才有意义
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

}
